import Vue, {set} from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
Vue.use(VueRouter)
import { Notification, MessageBox, Message, Loading } from 'element-ui'
import ElementUI from 'element-ui'

// 解决重复导航到同一路由的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') throw err
  })
}

// 导入区块链存储管理页面
import BlockchainStorage from '@/views/BlockchainStorage'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue'),
  },
  {
    path: '/404',
    name: '404',
    component: () => import(/* webpackChunkName: "about" */ '../views/404.vue'),
  },
  {
    path: '/stock',
    name: 'StockManagement',
    component: () => import(/* webpackChunkName: "stock" */ '../views/StockManagement.vue'),
  },
  {
    path: '/shop',
    name: 'Shop',
    component: () => import(/* webpackChunkName: "shop" */ '../views/Shop.vue'),
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "cart" */ '../views/Cart.vue'),
  },
  {
    path: '/checkout',
    name: 'Checkout',
    component: () => import(/* webpackChunkName: "checkout" */ '../views/Checkout.vue'),
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue'),
  },
  {
    path: '/farmer-product',
    name: 'FarmerProduct',
    component: () => import(/* webpackChunkName: "farmer" */ '../views/FarmerProduct.vue'),
  },
  {
    path: '/product-audit',
    name: 'ProductAudit',
    component: () => import(/* webpackChunkName: "admin" */ '../views/ProductAudit.vue'),
  },
  {
    path: '/front',
    name: 'front',
    redirect:'./front/home',
    component: () => import(/* webpackChunkName: "about" */ '../views/front/Front.vue'),
    children:[
      {
        path: 'home',
        name: 'home',
        component: () => import(/* webpackChunkName: "about" */ '../views/front/Home.vue'),
      },
      {
        path: 'password',
        name: 'password',
        component: () => import(/* webpackChunkName: "about" */ '../views/front/Password.vue'),
      },
      {
        path: 'person',
        name: 'person',
        component: () => import(/* webpackChunkName: "about" */ '../views/front/Person.vue'),
      },
      {
        path: 'quality',
        name: 'quality',
        component: () => import(/* webpackChunkName: "about" */ '../views/QualityMonitor.vue'),
      },
    ]
  },
  {
    path: '/quality',
    name: 'qualityMonitor',
    component: () => import(/* webpackChunkName: "about" */ '../views/QualityMonitor.vue'),
    meta: { requireAuth: true }
  },
  {
    path: '/logistics-monitor',
    name: 'LogisticsMonitor',
    component: () => import(/* webpackChunkName: "about" */ '../views/LogisticsMonitor.vue'),
    meta: { requireAuth: true }
  },
  {
    path: '/blockchainStorage',
    component: () => import(/* webpackChunkName: "manage" */ '../views/Manage.vue'),
    meta: {
      title: '区块链存储管理',
      requireAuth: true
    },
    children: [
      {
        path: '',
        name: 'BlockchainStorage',
        component: BlockchainStorage,
        meta: {
          title: '区块链存储管理',
          icon: 'el-icon-connection'
        }
      }
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//重置路由器里面的路由集合
export const resetRoutes = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

export const setRoutes  = () => {
  //获取浏览器缓存的菜单数据
  const localMenus = localStorage.getItem("menus") ;
  if(localMenus){
    const currentRoutes =  router.getRoutes().map(v => v.name);
    if(!currentRoutes.includes('manage')){
      //当前Router不包含manage，在拼装
      const manageRoute =  {
        path: '/',
        name: 'manage',
        redirect:'/home',
        component: () => import(/* webpackChunkName: "about" */ '../views/Manage.vue'),
        children:[
          {
            path: 'person',
            name: 'person',
            component: () => import(/* webpackChunkName: "about" */ '../views/Person.vue'),
          },
          {
            path: 'password',
            name: 'password',
            component: () => import(/* webpackChunkName: "about" */ '../views/Password.vue'),
          },
          {
            path: 'home',
            name: 'home',
            component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
          },
          {
            path: 'blockchainStorage',
            name: 'blockchainStorage',
            component: () => import(/* webpackChunkName: "blockchain" */ '../views/BlockchainStorage.vue'),
          },
          {
            path: 'shop',
            name: 'shop',
            component: () => import(/* webpackChunkName: "about" */ '../views/Shop.vue'),
          },
          {
            path: 'cart',
            name: 'cart',
            component: () => import(/* webpackChunkName: "about" */ '../views/Cart.vue'),
          },
          {
            path: 'checkout',
            name: 'checkout',
            component: () => import(/* webpackChunkName: "about" */ '../views/Checkout.vue'),
          },
          {
            path: 'order',
            name: 'order',
            component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue'),
          },
          {
            path: 'farmer-product',
            name: 'farmerProduct',
            component: () => import(/* webpackChunkName: "farmer" */ '../views/FarmerProduct.vue'),
          },
          {
            path: 'product-audit',
            name: 'productAudit',
            component: () => import(/* webpackChunkName: "admin" */ '../views/ProductAudit.vue'),
          }
        ]
      };
      const menus = JSON.parse(localMenus);
      menus.forEach(item => {
        if(item.path){
          const itemMenu = {
            path:item.path.replace("/",""),
            name:item.name,
            component: () => import(/* webpackChunkName: "about" */ '../views/' + item.pagePath.replace('system/', '') + '.vue'),
          };
          manageRoute.children.push(itemMenu);
        }else if(item.children.length){
          item.children.forEach(item => {
            const itemMenu = {
              path:item.path.replace("/",""),
              name:item.name,
              component: () => import(/* webpackChunkName: "about" */ '../views/' + item.pagePath.replace('system/', '') + '.vue'),
            };
            manageRoute.children.push(itemMenu);
          })
        }
      })
      router.addRoute(manageRoute);
      console.log('1---重新加入manage路由')
      console.log('1---重新加入manage路由')
      console.log('1---重新加入manage路由')
      console.log(router.getRoutes())
    }else{
      console.log('未重新加入manage路由')
      console.log('未重新加入manage路由')
      console.log('未重新加入manage路由')
      console.log(router.getRoutes())
    }
  }
}

setRoutes()

router.beforeEach((to,from,next) => {
  localStorage.setItem('currentPathName',to.name);
  store.commit('setPath')
  const localMenus = localStorage.getItem("menus");
  if(!to.matched.length){
    //没有匹配到路由（也就是未找到路由)
    if(localMenus){
      //用户登录了
      next('/404')
    }else{
      ElementUI.Message({
        message: '请先登录',
        type: 'warning'
      });
      next('/login')
    }
  }
  next();
})
export default router
